/*
 * @Author: Wangjun
 * @Date: 2021-05-15 22:46:27
 * @LastEditTime: 2023-02-20 15:17:28
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \godriver\opdb\example\getpoint\getpoint.go
 * hnxr
 */
package main

import (
	"io/ioutil"
	"log"
	"strings"

	"gitee.com/haodreams/libs/ee"
	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/haodreams/godriver/opdb/opsql"
	"gitee.com/haodreams/golib/env"
	"gitee.com/haodreams/golib/logs"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func main() {
	env.Reset()
	data, err := ioutil.ReadFile("conf.txt")
	if err != nil {
		log.Fatalln(err)
	}

	ss := strings.Split(string(data), ";")
	dataSource := strings.TrimSpace(ss[0])

	dialector := opsql.Open(dataSource)
	if dialector == nil {
		ee.NewError("不支持的驱动:" + dataSource).Once2(logs.CbErrorf)
		return
	}
	op, err := gorm.Open(dialector, &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})

	//查询服务器时间
	// tm := int64(0)
	// err = op.Raw("SELECT time").Get(&tm)
	// if err != nil {
	// 	log.Fatalln(err)
	// }
	// log.Println("服务器时间:", tm, time.Unix(tm, 0))

	if len(ss) < 2 {
		log.Fatalln("没有需要发送的数据")
	}

	ss = strings.Split(ss[1], ",")

	db, _ := op.DB()
	records, err := db.Query("READ POINT ID RT GN ED", ss)
	if err != nil {
		log.Println(err)
		return
	}

	var rows []*opdb.Point
	defer records.Close()
	if records.Next() {
		err = op.ScanRows(records, &rows)
		if err != nil {
			log.Println(err)
			return
		}
	} else {
		return
	}

	for _, row := range rows {
		log.Printf("%+v\n", row)

	}

	//op.Exec("REMOVE POINT", ss)

}
